package com.ciko.code.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.util.ArrayList;
import java.util.List;


public class Utils
{
	public static final String	  TAG				  = "PushDemoActivity";
	public static final String	  RESPONSE_METHOD	  = "method";
	public static final String	  RESPONSE_CONTENT	  = "content";
	public static final String	  RESPONSE_ERRCODE	  = "errcode";
	protected static final String ACTION_LOGIN		  = "com.baidu.pushdemo.action.LOGIN";
	public static final String	  ACTION_MESSAGE	  = "com.baiud.pushdemo.action.MESSAGE";
	public static final String	  ACTION_RESPONSE	  = "bccsclient.action.RESPONSE";
	public static final String	  ACTION_SHOW_MESSAGE = "bccsclient.action.SHOW_MESSAGE";
	protected static final String EXTRA_ACCESS_TOKEN  = "access_token";
	public static final String	  EXTRA_MESSAGE		  = "message";
													  
	public static String		  logStringCache	  = "";
													  
	// 获取ApiKey
	public static String getMetaValue(Context context, String metaKey) {
		Bundle metaData = null;
		String apiKey = null;
		if (context == null || metaKey == null) { return null; }
		try
		{
			ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
			if (null != ai)
			{
				metaData = ai.metaData;
			}
			if (null != metaData)
			{
				apiKey = metaData.getString(metaKey);
			}
		}
		catch (NameNotFoundException e)
		{
		
		}
		return apiKey;
	}
	
	public static List<String> getTagsList(String originalText) {
		if (originalText == null || originalText.equals("")) { return null; }
		List<String> tags = new ArrayList<String>();
		int indexOfComma = originalText.indexOf(',');
		String tag;
		while (indexOfComma != -1)
		{
			tag = originalText.substring(0, indexOfComma);
			tags.add(tag);
			
			originalText = originalText.substring(indexOfComma + 1);
			indexOfComma = originalText.indexOf(',');
		}
		
		tags.add(originalText);
		return tags;
	}
	
	public static String getLogText(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		return sp.getString("log_text", "");
	}
	
	public static void setLogText(Context context, String text) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = sp.edit();
		editor.putString("log_text", text);
		editor.commit();
	}
	
	/**
	 * 获取系统的UA
	 * 
	 * @param ctx
	 * @return
	 */
	public static String getUA(Context ctx) {
		String ua = null;
		WebView webview = new WebView(ctx);
		WebSettings settings = webview.getSettings();
		ua = settings.getUserAgentString();
		return ua;
	}
	
	/**
	 * 压缩图片
	 * 
	 * @param srcPath
	 * @return
	 */
	public static Bitmap compressImageFromFile(String srcPath) {
		BitmapFactory.Options newOpts = new BitmapFactory.Options();
		newOpts.inJustDecodeBounds = true;//只读边,不读内容  
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		
		newOpts.inJustDecodeBounds = false;
		int w = newOpts.outWidth;
		int h = newOpts.outHeight;
		float hh = 800f;//  
		float ww = 480f;//  
		int be = 1;
		if (w > h && w > ww)
		{
			be = (int) (newOpts.outWidth / ww);
		}
		else if (w < h && h > hh)
		{
			be = (int) (newOpts.outHeight / hh);
		}
		if (be <= 0)
		             be = 1;
		newOpts.inSampleSize = be;//设置采样率  
		
		newOpts.inPreferredConfig = Bitmap.Config.ARGB_8888;//该模式是默认的,可不设  
		newOpts.inPurgeable = true;// 同时设置才会有效  
		newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收  
		
		bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		return bitmap;
	}
	/**
	 * 隐藏键盘
	 */
	public static void hideInputMethodManager(Context mContext, View view) {
		final InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (inputMethodManager != null && view != null)
		{
			inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}

	/**
	 * 显示键盘
	 */
	public static InputMethodManager showInputMethodManager(Context mContext) {
		final InputMethodManager mInputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		mInputMethodManager.toggleSoftInput(InputType.TYPE_CLASS_PHONE, InputMethodManager.HIDE_NOT_ALWAYS);
		return mInputMethodManager;
	}
	
}
